﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneClientList : UserControl
    {
        private VodafoneController _vodafoneController;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.ClientSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public VodafoneClientList()
        {
            InitializeComponent();
            editToolStripMenuItem.Click += editToolStripMenuItem_Click;
            addToolStripMenuItem.Click += addToolStripMenuItem_Click;
            gridView.DoubleClick += gridView_DoubleClick;
            contextMenuStrip.Opening += contextMenuStrip_Opening;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _vodafoneController.Clients;
        }

        private static Cliente GetClone(Cliente client)
        {
            Cliente document = new Cliente();
            document.IdCliente = client.IdCliente;
            document.Nome = client.Nome;
            document.NIF = client.NIF;
            document.NumeroConta = client.NumeroConta;
            return document;
        }

        private static void Merge(Cliente client, Cliente document)
        {
            client.Nome = document.Nome;
            client.NIF = document.NIF;
            client.NumeroConta = document.NumeroConta;
        }

        private void ShowClient(Cliente client)
        {
            Cliente document = GetClone(client);
            VodafoneClient window = new VodafoneClient();
            window.Document = document;
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(client, window.Document);
            _vodafoneController.SaveClient(client);
        }

        #endregion

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            Cliente client = (Cliente)gridView.GetRow(gridView.FocusedRowHandle);
            if (client == null)
                return;
            ShowClient(client);
        }

        void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cliente client = (Cliente)gridView.GetRow(gridView.FocusedRowHandle);
            ShowClient(client);
        }

        void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cliente client = new Cliente();
            client.IdCliente = -1;
            client.Nome = string.Empty;
            ShowClient(client);
        }
       
        void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            Cliente client = (Cliente)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = client != null;
        } 
    }
}
